# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import unittest

from testing_support import trial_dir


class TestTrialDir(unittest.TestCase):
  def test_simple_workflow(self):
    self.assertEqual(trial_dir.TrialDir.TRIAL_ROOT, None)

    trial_dir1 = trial_dir.TrialDir('subdir1')
    trial_dir1.set_up()
    self.assertIsInstance(trial_dir1.TRIAL_ROOT, basestring)
    orig_trial_root = trial_dir1.TRIAL_ROOT
    self.assertTrue(
      os.path.isdir(os.path.join(trial_dir1.TRIAL_ROOT, 'subdir1')))

    trial_dir2 = trial_dir.TrialDir('subdir2')
    trial_dir2.set_up()
    self.assertEqual(trial_dir2.TRIAL_ROOT, orig_trial_root)
    self.assertTrue(os.path.isdir(trial_dir2.root_dir))

    trial_dir2.tear_down()
    self.assertTrue(os.path.isdir(trial_dir1.root_dir))
    self.assertFalse(
      os.path.isdir(os.path.join(trial_dir2.TRIAL_ROOT, 'subdir2')))

    trial_dir1.tear_down()
    self.assertFalse(
      os.path.isdir(os.path.join(trial_dir1.TRIAL_ROOT, 'subdir1')))

    # Not supposed to be called directly, but we're testing.
    trial_dir1._clean()
    self.assertFalse(os.path.isdir(orig_trial_root))


class TestTrialDirMixIn(unittest.TestCase, trial_dir.TrialDirMixIn):
  def setUp(self):
    trial_dir.TrialDirMixIn.setUp(self)
    self.mixin_root_dir = self.root_dir

  def test_mixin(self):
    self.assertTrue(os.path.isdir(self.root_dir))

  def tearDown(self):
    trial_dir.TrialDirMixIn.tearDown(self)
    self.assertIsNone(self.root_dir)
    self.assertFalse(os.path.isdir(self.mixin_root_dir))


class TestTrialDirTestCase(trial_dir.TestCase):
  def setUp(self):
    trial_dir.TestCase.setUp(self)
    self.mixin_root_dir = self.root_dir

  def test_root_dir(self):
    self.assertTrue(os.path.isdir(self.root_dir))
    # Only testing TrialDir here, auto_stub is tested elsewhere.

  def tearDown(self):
    trial_dir.TestCase.tearDown(self)
    self.assertIsNone(self.root_dir)
    self.assertFalse(os.path.isdir(self.mixin_root_dir))
